package com.sihan.swvp.action.jc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sihan.swvp.action.BaseAction;
import com.sihan.swvp.entity.jc.JcBzjsbzzy;
import com.sihan.swvp.service.jc.JcBdzService;
import com.sihan.swvp.service.jc.JcBzService;
import com.sihan.swvp.service.jc.JcBzjsService;
import com.sihan.swvp.service.jc.JcBzjsbzzyService;

@Controller
@RequestMapping("/jc/JcBzjsbzzyAction")
public class JcBzjsbzzyAction extends BaseAction{

	@Autowired
	private JcBzjsbzzyService jcBzjsbzzyService;
	@Autowired
	private JcBzService jcBzService;
	@Autowired
	private JcBzjsService jcBzjsService;
	@Autowired
	private JcBdzService jcBdzService;
	
	@ResponseBody
	@RequestMapping("/getBzzyZygcByIdList")
	public Object getJcBzjsbzzy(@Param("jcBzjsId")String jcBzjsId,@Param("page")Integer page,@Param("limit")Integer limit){
		if(jcBzjsId !=null && !"".equals(jcBzjsId)){
			Map<String, Object> params=new HashMap<String, Object>();
			params.put("jcBzjsId", jcBzjsId);
			List<Map<String, Object>> result=jcBzjsbzzyService.getBzzyZygcByIdList(params);
			return result;
		}else{
			return "";
		}
	}
	
	@ResponseBody
	@RequestMapping("/getJcBzjsbzzyByIdList")
	public Object getJcBzjsbzzyByIdList(@Param("jcBzjsId")String jcBzjsId){
		if(jcBzjsId !=null && !"".equals(jcBzjsId)){
			Map<String, Object> params=new HashMap<String, Object>();
			params.put("jcBzjsId", jcBzjsId);
			List<Map<String, Object>> result=jcBzjsbzzyService.getJcBzjsbzzyByIdList(params);
			return result;
		}else{
			return "";
		}
	}
	
	@ResponseBody
	@RequestMapping("/getJcBzjsbzzyTree") 
	public List<Map<String, Object>> getJcBzjsbzzyTree(@Param("node")String node){
		Map<String, Object> params = new HashMap<String, Object>();
		List<Map<String, Object>> resultlist= new ArrayList<Map<String,Object>>();
		
		List<Map<String, Object>> bdzList=jcBdzService.getJcBdzAll(params);//得到变电站数据
		for (int y = 0; y < bdzList.size(); y++) {
			Map<String, Object> bzdMapC=new HashMap<String, Object>();
			bzdMapC.put("text", bdzList.get(y).get("text"));
			
			params.put("bdzId", bdzList.get(y).get("id"));
			List<Map<String, Object>> bzlist = jcBzService.getJcBzTree(params);//得到班组的的数据
			
			List<Map<String, Object>> twolist= new ArrayList<Map<String,Object>>();
			for (int x = 0; x < bzlist.size(); x++) {//父节点循环
				 Map<String, Object> twoMap=new HashMap<String, Object>();
				 twoMap.put("text", bzlist.get(x).get("text"));
				 twoMap.put("id", bzlist.get(x).get("id"));
				
				 params.put("jcBzId", bzlist.get(x).get("id"));
				 List<Map<String, Object>> jsData=jcBzjsService.getJcBzjs(params);//得到班组角色的数据
				 List<Map<String, Object>> onelist=new ArrayList<Map<String,Object>>();
				 for (int i = 0; i < jsData.size(); i++) {
					 Map<String, Object> mapjs=new HashMap();
					 mapjs.put("text", jsData.get(i).get("bzjsName"));
					 mapjs.put("pid", jsData.get(i).get("id"));
					 mapjs.put("leaf",true);
					 onelist.add(mapjs);
				}
				 twoMap.put("children", onelist);
				 twolist.add(twoMap);
			}
			bzdMapC.put("children", twolist);
			resultlist.add(bzdMapC);
		}
		return resultlist;
	}
	
	
	@ResponseBody
	@RequestMapping("/addJcBzjsbzzy")
	public Object addJcBzjsbzzy(@Param("jcBzjsId")int jcBzjsId,@Param("jcBzzyZygcIds")String jcBzzyZygcIds){
		try {
			jcBzjsbzzyService.deleteJcBzjsbzzy(jcBzjsId);
			if(jcBzzyZygcIds !=null && !"".equals(jcBzzyZygcIds)){
				String[] idsarray = jcBzzyZygcIds.split(",");
				jcBzjsbzzyService.addJcBzjsbzzy(jcBzjsId,getLoginUser().getId(),idsarray);
			}
			return successMsg("");
		} catch (Exception e) {
			e.printStackTrace();
			return failMsg("服务器异常");
		}
	}
	
	@ResponseBody
	@RequestMapping("/updateJcBzjsbzzy")
	public Object updateJcBzjsbzzy(JcBzjsbzzy bean){
		try {
			bean.setUpuser(getLoginUser().getId());
			jcBzjsbzzyService.updateJcBzjsbzzy(bean);
			return successMsg("");
		} catch (Exception e) {
			e.printStackTrace();
			return failMsg("服务器异常");
		}
	}
	
}
